﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 内容模型 字段管理
//最后修改: 2010-10-11 修改Save 增加修改数据表 功能
//最后修改: 2010-10-21李晓忠  增加GetWithTableNameListByCondition功能
//最后修改: 2010-11-16李晓忠  增加获取标题字段功能
//最后修改: 2010-11-19 李晓忠  添加获取主内容字段
//最后修改: 2010-11-25 李晓忠  添加获取搜索字段
using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;
using System.Data;
using TRPOP.Common;
using TRPOP.Config;
using TRPOP.CMS.Model;
using TRPOP.CMS.DALFactory;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace TRPOP.CMS.DALSQLServer
{
    public class ModelFieldDAL : IModelFieldDAL
    {

        #region 保存 Save
        public EnumSubmitResult Save(ModelFieldInfo info)
        {

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            if (info.ModelFieldId > 0)
            {
                #region  修改
                ModelInfo oModelInfo = (new ModelDAL()).GetInfoById(info.ModelId);
                ModelFieldInfo oOldModelFieldInfo = GetInfoById(info.ModelFieldId);
                if (info.FieldName == oOldModelFieldInfo.FieldName &&
                    info.FieldTypeStr == oOldModelFieldInfo.FieldTypeStr &&
                    info.FieldLength == oOldModelFieldInfo.FieldLength &&
                    info.DefaultValue == oOldModelFieldInfo.DefaultValue
                    )
                {
                    //不需要修改数据表
                    enmResult = EnumSubmitResult.Success;
                }
                else
                {
                    #region 修改数据表
                    string strSQL = "";
                    //删除原有的默认值
                    if (oOldModelFieldInfo.DefaultValue != "")
                    {
                        strSQL += "alter table " + SystemInfoHelper.getSystemInfo().DBOwner + ".{0} drop constraint  DF_{0}_{1} {2}";
                        strSQL = string.Format(
                                   strSQL,
                                   oModelInfo.TableName,
                                   oOldModelFieldInfo.FieldName,
                                   System.Environment.NewLine
                               );
                    }
                    //修改 字段名
                    if (oOldModelFieldInfo.FieldName != info.FieldName)
                    {
                        strSQL += "sp_rename N'" + SystemInfoHelper.getSystemInfo().DBOwner + ".{0}.{1}', N'{2}', 'COLUMN' {3}";
                        strSQL = string.Format(strSQL,
                                               oModelInfo.TableName,
                                               oOldModelFieldInfo.FieldName,
                                               info.FieldName,
                                               System.Environment.NewLine
                                               );
                    }
                    //类型 长度
                    if (oOldModelFieldInfo.FieldTypeStr != info.FieldTypeStr ||
                        oOldModelFieldInfo.FieldLength != info.FieldLength
                        )
                    {
                        strSQL += "alter table " + SystemInfoHelper.getSystemInfo().DBOwner + ".{0} alter column {1}  {2}  {3} {4}";
                        strSQL = string.Format(
                                      strSQL,
                                      oModelInfo.TableName,
                                      info.FieldName,
                                      info.FieldTypeStr,
                                      TableFieldInfo.GetLengthLimit(info.FieldTypeStr, info.FieldLength),
                                      System.Environment.NewLine
                                      );
                    }
                    //默认值
                    string strDefault = TableFieldInfo.PutDeafultValue(info.FieldTypeStr, info.DefaultValue);
                    if (strDefault != "")
                    {
                        strSQL += " ALTER TABLE " + SystemInfoHelper.getSystemInfo().DBOwner + ".{0} ADD CONSTRAINT {3} DF_{0}_{1} DEFAULT {2} FOR {1}  ";
                        strSQL = string.Format(strSQL, oModelInfo.TableName, info.FieldName, strDefault, System.Environment.NewLine);
                    }

                    SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                    DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSQL);
                    try
                    {
                        odbDataBase.ExecuteNonQuery(odbCommand);
                        enmResult = EnumSubmitResult.Success;
                    }
                    catch //(Exception oeException)
                    {
                        //throw new Exception(oeException.Message, oeException);
                    }
                    #endregion
                }
                #endregion
            }
            else
            {
                #region  增加
                ModelInfo oModelInfo = (new ModelDAL()).GetInfoById(info.ModelId);
                string strSQL = "ALTER TABLE " + SystemInfoHelper.getSystemInfo().DBOwner + ".{0} ADD {1}" +
                             "{2} {3}{4} {5}";
                strSQL = string.Format(strSQL,
                              oModelInfo.TableName,
                              System.Environment.NewLine,
                              info.FieldName,
                              info.FieldTypeStr,
                              TableFieldInfo.GetLengthLimit(info.FieldTypeStr, info.FieldLength),
                              "NULL");
                string strDefault = TableFieldInfo.PutDeafultValue(info.FieldTypeStr, info.DefaultValue);
                if (strDefault != "")
                {
                    strSQL += System.Environment.NewLine;
                    strSQL += " ALTER TABLE " + SystemInfoHelper.getSystemInfo().DBOwner + ".{0} ADD CONSTRAINT " + System.Environment.NewLine +
                              " DF_{0}_{1} DEFAULT {2} FOR {1}  ";
                    strSQL = string.Format(strSQL, oModelInfo.TableName, info.FieldName, strDefault);
                }


                SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSQL);
                try
                {
                    odbDataBase.ExecuteNonQuery(odbCommand);
                    enmResult = EnumSubmitResult.Success;
                }
                catch //(Exception oeException)
                {
                    //throw new Exception(oeException.Message, oeException);
                }
                #endregion
            }
            if (enmResult == EnumSubmitResult.Success)
            {
                #region 字段表 增加相关的数据
                string strStoreProcedure = SystemInfoHelper.getSystemInfo().DBprefixTableName + "_" + "ModelField_Save";
                SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);

                odbDataBase.AddInParameter(odbCommand, "@FormInputTypeId", System.Data.DbType.Int32, info.FormInputTypeId);
                odbDataBase.AddInParameter(odbCommand, "@FormInputLimitTypeId", System.Data.DbType.Int32, info.FormInputLimitTypeId);
                odbDataBase.AddInParameter(odbCommand, "@FormInputValueTypeId", System.Data.DbType.Int32, info.FormInputValueTypeId);
                odbDataBase.AddInParameter(odbCommand, "@ModelId", System.Data.DbType.Int32, info.ModelId);
                odbDataBase.AddInParameter(odbCommand, "@Name", System.Data.DbType.String, info.Name);
                odbDataBase.AddInParameter(odbCommand, "@FieldName", System.Data.DbType.String, info.FieldName);
                odbDataBase.AddInParameter(odbCommand, "@FieldTypeStr", System.Data.DbType.String, info.FieldTypeStr);
                odbDataBase.AddInParameter(odbCommand, "@FieldLength", System.Data.DbType.Int32, info.FieldLength);
                odbDataBase.AddInParameter(odbCommand, "@DefaultValue", System.Data.DbType.String, info.DefaultValue);
                odbDataBase.AddInParameter(odbCommand, "@DisplayOrder", System.Data.DbType.Int32, info.DisplayOrder);
                odbDataBase.AddInParameter(odbCommand, "@IsContribute", System.Data.DbType.Boolean, info.IsContribute);
                odbDataBase.AddInParameter(odbCommand, "@IsSearch", System.Data.DbType.Boolean, info.IsSearch);
                odbDataBase.AddInParameter(odbCommand, "@IsCollect", System.Data.DbType.Boolean, info.IsCollect);
                odbDataBase.AddInParameter(odbCommand, "@IsEdit", System.Data.DbType.Boolean, info.IsEdit);
                odbDataBase.AddInParameter(odbCommand, "@IsTitleField", System.Data.DbType.Boolean, info.IsTitleField);
                odbDataBase.AddInParameter(odbCommand, "@IsMainField", System.Data.DbType.Boolean, info.IsMainField);
                odbDataBase.AddInParameter(odbCommand, "@FieldListDisplay", System.Data.DbType.Boolean, info.FieldListDisplay);
                odbDataBase.AddInParameter(odbCommand, "@FieldInputPicker", System.Data.DbType.String, info.FieldInputPicker);
                odbDataBase.AddParameter(odbCommand, "@ModelFieldId", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, info.ModelFieldId);

                enmResult = EnumSubmitResult.Failed;
                try
                {
                    odbDataBase.ExecuteNonQuery(odbCommand);
                    info.ModelFieldId = (int)odbDataBase.GetParameterValue(odbCommand, "@ModelFieldId");
                    enmResult = EnumSubmitResult.Success;
                }
                catch (Exception oeException)
                {
                    throw new Exception(oeException.Message, oeException);
                }
                #endregion
            }
            return enmResult;
        }

        #endregion

        #region 获取实体 GetInfoById
        public ModelFieldInfo GetInfoById(int id)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField WHERE ModelFieldId=" + id;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            ModelFieldInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new ModelFieldInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }
        #endregion

        #region 删除 DeleteInfoById
        public EnumSubmitResult DeleteInfoById(int id)
        {
            ModelFieldInfo oModelFieldInfo = GetInfoById(id);
            ModelInfo oModelInfo = (new ModelDAL()).GetInfoById(oModelFieldInfo.ModelId);
            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            string strSQL = "";
            //删除原有的默认值
            if (oModelFieldInfo.DefaultValue != "")
            {
                strSQL += "ALTER TABLE " + SystemInfoHelper.getSystemInfo().DBOwner + ".{0} drop constraint  DF_{0}_{1} {2}  ";
                strSQL = string.Format(
                           strSQL,
                           oModelInfo.TableName,
                           oModelFieldInfo.FieldName,
                           System.Environment.NewLine
                       );
            }
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = null;
            if (strSQL.IsEmpty() == false)
            {
                odbCommand = odbDataBase.GetSqlStringCommand(strSQL);
                try
                {
                    odbDataBase.ExecuteNonQuery(odbCommand);
                }
                catch //(Exception oeException)
                {
                    //throw new Exception(oeException.Message, oeException);
                }
            }

            strSQL = string.Format("  ALTER TABLE {1} DROP COLUMN {0}", oModelFieldInfo.FieldName, oModelInfo.TableName);
            odbCommand = odbDataBase.GetSqlStringCommand(strSQL);
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                enmResult = EnumSubmitResult.Success;
            }
            catch //(Exception oeException)
            {
                //throw new Exception(oeException.Message, oeException);
            }
            if (enmResult == EnumSubmitResult.Success)
                return DeleteInfoByIds(id.ToString());
            else
                return EnumSubmitResult.Failed;
        }
        #endregion

        #region 删除 DeleteInfoByIds
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            string strSqlString = "DELETE FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField WHERE ModelFieldId in(" + ids + ")";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }
        #endregion

        #region 自定义列表（无分页） GetListByCondition
        public List<ModelFieldInfo> GetListByCondition(int topn, string condition, string orderby)
        {
            string strSqlString = "SELECT ";
            if (topn > 0)
            {
                strSqlString += " TOP " + topn;
            }
            strSqlString += " * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField WHERE 1=1"; if (condition != "")
            {
                strSqlString += condition;
            }
            if (orderby != "")
            {
                strSqlString += " ORDER BY " + orderby;
            }
            else
            {
                strSqlString += " ORDER BY ModelFieldId DESC";
            }

            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            List<ModelFieldInfo> olstInfo = new List<ModelFieldInfo>(); ModelFieldInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new ModelFieldInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion

        #region 自定义列表（分页） GetListByPager
        public List<ModelFieldInfo> GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            string strStoreProcedure = SystemInfoHelper.getSystemInfo().PagerStoreProcedureName;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);
            List<ModelFieldInfo> olstInfo = new List<ModelFieldInfo>(); ModelFieldInfo oInfo = null;
            odbDataBase.AddInParameter(odbCommand, "@TableName", System.Data.DbType.String, SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField");
            odbDataBase.AddInParameter(odbCommand, "@ShowColumns", System.Data.DbType.String, "*");
            odbDataBase.AddInParameter(odbCommand, "@pageNumber", System.Data.DbType.Int32, pageNumber);
            odbDataBase.AddInParameter(odbCommand, "@pageSize", System.Data.DbType.Int32, pageSize);
            odbDataBase.AddInParameter(odbCommand, "@condition", System.Data.DbType.String, condition);
            odbDataBase.AddInParameter(odbCommand, "@orderby", System.Data.DbType.String, orderby);
            odbDataBase.AddOutParameter(odbCommand, "@recordCount", System.Data.DbType.Int32, 0);
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new ModelFieldInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                }
                recordCount = int.Parse(odbCommand.Parameters["@RecordCount"].Value.ToString());
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion

        #region 自定义列表（无分页） GetWithTableNameListByCondition
        private List<ModelFieldInfoWithTableName> GetWithTableNameListByCondition(int topn, string condition, string orderby)
        {
            string strSqlString = "SELECT ";
            if (topn > 0)
            {
                strSqlString += " TOP " + topn;
            }
            strSqlString += " a.* ,b.TableName FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField as a left join " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_Model as b on a.ModelId=b.ModelId ";
            if (condition != "")
            {
                strSqlString += " Where 1=1 " + condition;
            }
            if (orderby != "")
            {
                strSqlString += " ORDER BY " + orderby;
            }
            else
            {
                strSqlString += " ORDER BY ModelFieldId DESC";
            }

            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            List<ModelFieldInfoWithTableName> olstInfo = new List<ModelFieldInfoWithTableName>(); ModelFieldInfoWithTableName oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new ModelFieldInfoWithTableName();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion

        #region 自定义列表（无分页） GetWithTableNameListByModelId
        public List<ModelFieldInfoWithTableName> GetWithTableNameListByModelId(int ModelId)
        {
            string condition = " AND a.ModelId=" + ModelId;
            string orderby = " a.DisplayOrder , a.ModelFieldId  asc";
            return GetWithTableNameListByCondition(0, condition, orderby);

        }
        #endregion

        #region 自定义列表（无分页） GetWithTableNameListByNodeId
        public List<ModelFieldInfoWithTableName> GetWithTableNameListByNodeId(int NodeId)
        {
            string condition = " AND a.ModelId= (SELECT ModelId FROM  " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_node_Node WHERE NodeId=" + NodeId + ")";
            string orderby = " a.DisplayOrder asc";
            return GetWithTableNameListByCondition(0, condition, orderby);

        }
        #endregion

        #region 模型 列表显示字段列表（无分页） GetTableFieldListDisplayListByNodeId
        public List<ModelFieldInfo> GetTableFieldListDisplayListByNodeId(int NodeId)
        {
            string condition = " AND FieldListDisplay=1  AND ModelId= (SELECT ModelId FROM  " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_node_Node WHERE NodeId=" + NodeId + ")";
            string orderby = "  DisplayOrder asc";
            return GetListByCondition(0, condition, orderby);

        }
        #endregion

        #region 获取标题字段
        /// <summary>
        /// 获取标题字段
        /// </summary>
        /// <param name="NodeId"></param>
        /// <returns></returns>
        public ModelFieldInfo GetTableTitleFieldByNodeId(int NodeId)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField WHERE  IsTitleField=1  AND ModelId= (SELECT ModelId FROM  " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_node_Node WHERE NodeId=" + NodeId + ")";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            ModelFieldInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new ModelFieldInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }
        /// <summary>
        /// 获取标题字段
        /// </summary>
        /// <param name="ModelId"></param>
        /// <returns></returns>
        public ModelFieldInfo GetTableTitleFieldByModelId(int ModelId)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField WHERE  IsTitleField=1  AND ModelId= " + ModelId;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            ModelFieldInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new ModelFieldInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }
        #endregion

        #region 获取主内容字段
        /// <summary>
        /// 获取主内容字段
        /// </summary>
        /// <param name="NodeId"></param>
        /// <returns></returns>
        public ModelFieldInfo GetTableMainFieldByNodeId(int NodeId)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField WHERE  IsMainField=1  AND ModelId= (SELECT ModelId FROM  " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_node_Node WHERE NodeId=" + NodeId + ")";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            ModelFieldInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new ModelFieldInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }
        /// <summary>
        /// 获取主内容字段
        /// </summary>
        /// <param name="ModelId"></param>
        /// <returns></returns>
        public ModelFieldInfo GetTableMainFieldByModelId(int ModelId)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_ModelField WHERE  IsMainField=1  AND ModelId= " + ModelId;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            ModelFieldInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new ModelFieldInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }
        #endregion

        #region 更新标题字段
        public EnumSubmitResult UpdateTitleFiled(int id)
        {
            string strSqlString = string.Format(
                                            "  UPDATE [{0}_model_ModelField] " +
                                            "  SET  IsTitleField= 'false' " +
                                            "  WHERE ModelId=(select ModelId from [{0}_model_ModelField] where ModelFieldId={1}) " +
                                            "  update [{0}_model_ModelField] SET  IsTitleField= 'true' where ModelFieldId={1}"
                                        , SystemInfoHelper.getSystemInfo().DBprefixTableName, id);

            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }
        #endregion

        #region 更新主内容字段
        public EnumSubmitResult UpdateMainField(int id)
        {
            string strSqlString = string.Format(
                                            "  UPDATE [{0}_model_ModelField] " +
                                            "  SET  IsMainField= 'false' " +
                                            "  WHERE ModelId=(select ModelId from [{0}_model_ModelField] where ModelFieldId={1}) " +
                                            "  update [{0}_model_ModelField] SET  IsMainField= 'true' where ModelFieldId={1}"
                                        , SystemInfoHelper.getSystemInfo().DBprefixTableName, id);

            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }
        #endregion


        #region 更新主内容字段
        public EnumSubmitResult UpdateField(int id, string type)
        {
            string strSqlString = string.Format(
                                            "  update [{0}_model_ModelField]  " +
                                            "  set {1}=" +
                                            "  (case when {1}='true' then 'false' else 'true' end) " +
                                            "  where ModelFieldId={2}",
                                        SystemInfoHelper.getSystemInfo().DBprefixTableName, type, id);

            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }
        #endregion

        #region 获取搜索字段
        /// <summary>
        /// 获取搜索字段
        /// </summary>
        /// <param name="NodeId"></param>
        /// <returns></returns>
        public List<ModelFieldInfo> GetTableSearchFieldListByNodeId(int NodeId)
        {
            string condition = " AND IsSearch=1  AND ModelId= (SELECT ModelId FROM  " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_node_Node WHERE NodeId=" + NodeId + ")";
            string orderby = "  DisplayOrder asc";
            return GetListByCondition(0, condition, orderby);

        }
        #endregion


    }
}
